package com.sgzn.sgpay.web.controller;

import com.alibaba.fastjson.JSONArray;
import com.sgzn.sgpay.model.request.SgsMerchantDiscountRuleAddRequest;
import com.sgzn.sgpay.model.request.SgsMerchantDiscountRuleQueryRequest;
import com.sgzn.sgpay.model.request.SgsMerchantDiscountRuleUpdateRequest;
import com.sgzn.sgpay.model.request.dto.DiscountRuleDto;
import com.sgzn.sgpay.model.response.SgsResultResponse;
import com.sgzn.sgpay.persistence.domain.TMerchantDiscountRule;
import com.sgzn.sgpay.service.SgsMerchantDiscountRuleService;
import com.sgzn.sgpay.web.controller.base.MerchantBaseController;
import com.sgzn.sgpay.web.handle.AssembleCommonHandle;
import com.sgzn.sgpay.web.request.Impl.MerchantDiscountRuleAddRequest;
import com.sgzn.sgpay.web.request.Impl.MerchantDiscountRuleQueryRequest;
import com.sgzn.sgpay.web.request.Impl.MerchantDiscountRuleUpdateRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * 商户消费优惠规则控制器
 * @author huangsenguang
 * @version Id: MerchantDiscountRuleController.java, v 0.1 2018/5/1 10:59 huangsenguang Exp $$
 */
@Slf4j
@Controller
@RequestMapping("/merchant/discountRule")
public class MerchantDiscountRuleController extends MerchantBaseController {
    @Autowired
    private SgsMerchantDiscountRuleService sgsMerchantDiscountRuleService;
    @Resource
    private AssembleCommonHandle assembleCommonHandle;

    /**
     * 添加商户消费优惠规则
     * @param request
     * @return
     */
    @RequestMapping("add")
    @ResponseBody
    public SgsResultResponse<String> addMerchantDiscountRule(MerchantDiscountRuleAddRequest request) {
        super.setMchInfo(request);
        return this.sgsMerchantDiscountRuleService.addMerchantDiscountRule(assembleAddRequest(request));
    }

    /**
     * 查询商户消费优惠规则的信息
     * @param request
     * @return
     */
    @RequestMapping("queryList")
    @ResponseBody
    public SgsResultResponse<List<TMerchantDiscountRule>> queryMerchantDiscountList(MerchantDiscountRuleQueryRequest request) {
        super.setMchInfo(request);
        return this.sgsMerchantDiscountRuleService.queryMerchantDiscountList(assembleQueryRequest(request));
    }

    /**
     * 更新商户消费优惠规则信息
     * @param request
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public SgsResultResponse<String> updateMerchantDiscountRule(MerchantDiscountRuleUpdateRequest request) {
        super.setMchInfo(request);
        return this.sgsMerchantDiscountRuleService.updateMerchantDiscountRule(assembleUpdateRequest(request));
    }

    private SgsMerchantDiscountRuleAddRequest assembleAddRequest(MerchantDiscountRuleAddRequest request) {
        SgsMerchantDiscountRuleAddRequest sgsMerchantDiscountRuleAddRequest = new SgsMerchantDiscountRuleAddRequest();
        this.assembleCommonHandle.assemble(request, sgsMerchantDiscountRuleAddRequest);
        return sgsMerchantDiscountRuleAddRequest;
    }

    private SgsMerchantDiscountRuleQueryRequest assembleQueryRequest(MerchantDiscountRuleQueryRequest request) {
        SgsMerchantDiscountRuleQueryRequest sgsMerchantDiscountRuleQueryRequest = new SgsMerchantDiscountRuleQueryRequest();
        this.assembleCommonHandle.assemble(request, sgsMerchantDiscountRuleQueryRequest);
        return sgsMerchantDiscountRuleQueryRequest;
    }

    private SgsMerchantDiscountRuleUpdateRequest assembleUpdateRequest(MerchantDiscountRuleUpdateRequest request) {
        SgsMerchantDiscountRuleUpdateRequest sgsMerchantDiscountRuleRequest = new SgsMerchantDiscountRuleUpdateRequest();
        this.assembleCommonHandle.assemble(request, sgsMerchantDiscountRuleRequest);
        List<DiscountRuleDto> ruleList = JSONArray.parseArray(request.getRulesJson(), DiscountRuleDto.class);
        sgsMerchantDiscountRuleRequest.setRuleList(ruleList);
        return sgsMerchantDiscountRuleRequest;
    }

}

